package com.bamboo.tool.template.yapi.config;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.bamboo.tool.config.RestfulCloudApplicationCache;
import com.bamboo.tool.config.model.ApplicationConfig;
import com.bamboo.tool.template.torna.entity.LoginToken;
import com.bamboo.tool.template.torna.entity.TornaRespBase;
import com.bamboo.tool.template.torna.entity.TornaSetting;
import com.bamboo.tool.template.torna.util.TornaRequestUtil;
import com.bamboo.tool.template.yapi.entity.YapiSetting;
import com.bamboo.tool.template.yapi.util.YapiRequestUtil;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.util.NlsContexts;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class YapiConfigurable implements Configurable {
    private JTextField yapiUrlField;
    private JLabel yapiUrlLabel;
    private JPanel mainPanel;
    private JTextField usernameField;
    private JLabel userNameField;
    private JLabel passwordLabel;
    private JButton loginButton;
    private JLabel loginLabel;
    private JPasswordField passwordField;
    private JPanel loginPanel;

    @Override
    public String getDisplayName() {
        return "Torna Setting";
    }

    @Override
    public @Nullable JComponent createComponent() {

        ApplicationConfig state = RestfulCloudApplicationCache.getInstance().getState();
        YapiSetting yapiSetting = state.getYapiSetting();
        yapiUrlField.setText(yapiSetting.getUrl());
        usernameField.setText(yapiSetting.getUserName());
        passwordField.setText(yapiSetting.getPassword());
        //url listenter
        urlChangeListener(yapiSetting);
        // username listenter
        usernameChangeListener(yapiSetting);
        //password listenter
        passwordChangeListener(yapiSetting);
        // longin listenter
        loginListener(yapiSetting);
        try {
            YapiRequestUtil.login(yapiSetting);
            loginLabel.setForeground(Color.green);
            loginLabel.setText("成功！");
        } catch (Exception exception) {
            loginLabel.setForeground(Color.red);
            loginLabel.setText(exception.getMessage());
        }
        return mainPanel;
    }

    private void loginListener(YapiSetting state) {
        loginButton.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

                if (StrUtil.isEmpty(yapiUrlField.getText())) {
                    loginLabel.setForeground(Color.red);
                    loginLabel.setText("地址不能为空");
                    return;
                }

                if (StrUtil.isEmpty(usernameField.getText())) {
                    loginLabel.setForeground(Color.red);
                    loginLabel.setText("用户名不能为空");
                    return;
                }
                char[] passwordChar = passwordField.getPassword();
                String password = new String(passwordChar);
                if (StrUtil.isEmpty(password)) {
                    loginLabel.setForeground(Color.red);
                    loginLabel.setText("密码不能为空");
                    return;
                }

                state.setUrl(yapiUrlField.getText());
                state.setUserName(usernameField.getText());
                state.setPassword(password);
                try {
                    YapiRequestUtil.login(state);
                } catch (Exception exception) {
                    loginLabel.setForeground(Color.red);
                    loginLabel.setText(exception.getMessage());
                }


            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }

    private void urlChangeListener(YapiSetting setting) {
        yapiUrlField.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                setting.setUrl(yapiUrlField.getText());
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                setting.setUrl(yapiUrlField.getText());
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                setting.setUrl(yapiUrlField.getText());
            }
        });
    }

    private void usernameChangeListener(YapiSetting setting) {
        usernameField.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                setting.setUserName(usernameField.getText());
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                setting.setUserName(usernameField.getText());
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                setting.setUserName(usernameField.getText());
            }
        });
    }

    private void passwordChangeListener(YapiSetting state) {
        passwordField.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                char[] password = passwordField.getPassword();
                state.setPassword(new String(password));
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                char[] password = passwordField.getPassword();
                state.setPassword(new String(password));
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                char[] password = passwordField.getPassword();
                state.setPassword(new String(password));
            }
        });
    }

    @Override
    public boolean isModified() {

        return false;
    }

    @Override
    public void apply() throws ConfigurationException {

    }
}
